13 April 2015

Library ggmap

library(ggmap)
qmap("Mannheim")

MA_map <- qmap("Mannheim",zoom=14)

Other zoom level

qmap(location = 'Mannheim', zoom = 12)

Get closer

qmap(location = 'Mannheim', zoom = 13)

Get very close

qmap(location = 'Mannheim', zoom = 20)

ggmap - source

qmap(location = 'Mannheim', zoom = 14, source="osm")

ggmap - maptype

qmap(location = 'Mannheim', zoom = 14, maptype="satellite")

ggmap - maptype

qmap(location = 'Mannheim', zoom = 14, maptype="hybrid")

ggmap - maptype

qmap(location = 'Mannheim', zoom = 14,
 maptype="toner",source="stamen")

Other ways to get a map

  • qmap is a wrapper for ggmap and get_map
  • with get_googlemap you get similar results
MAmap <- get_googlemap("Mannheim")
ggmap(MAmap)

Geocoding

Geocoding (…) uses a description of a location, most typically a postal address or place name, to find geographic coordinates from spatial reference data …

Wikipedia - Geocoding

geocode("Mannheim University")
lon lat
8.462233 49.48371

Geocoding - points of interest

POI1 <- geocode("B2, 1 Mannheim")
POI2 <- geocode("Hbf Mannheim")
POI3 <- geocode("Wasserturm Mannheim")

Geocoding - with a loop

POI <- c("B2, 1 Mannheim","Hbf Mannheim",
         "Wasserturm Mannheim")

ListPOI <- data.frame(lat=NA,lon=NA)

for ( i in 1:length(POI)){
  geoPOI <- geocode(POI[i])
  ListPOI[i,"lat"] <-  geoPOI$lat 
  ListPOI[i,"lon"] <-  geoPOI$lon 
}

Points in map

More about adding points

ggmap - adding colors

ListPOI$color <- c("A","B","C")
MA_map +
geom_point(aes(x = lon, y = lat,col=color),
data = ListPOI)

ggmap - bigger dots

ListPOI$size <- c(10,20,30)
MA_map +
geom_point(aes(x = lon, y = lat,col=color,size=size),
data = ListPOI)

Cheatsheet

Get the distance between 2 points

mapdist("Q1, 4 Mannheim","B2, 1 Mannheim")
##             from             to   m    km     miles seconds  minutes
## 1 Q1, 4 Mannheim B2, 1 Mannheim 746 0.746 0.4635644     200 3.333333
##        hours
## 1 0.05555556
mapdist("Q1, 4 Mannheim","B2, 1 Mannheim",mode="walking")
##             from             to   m    km     miles seconds  minutes
## 1 Q1, 4 Mannheim B2, 1 Mannheim 546 0.546 0.3392844     421 7.016667
##       hours
## 1 0.1169444

Get another distance

mapdist("Q1, 4 Mannheim","B2, 1 Mannheim",mode="bicycling")
##             from             to   m    km    miles seconds  minutes
## 1 Q1, 4 Mannheim B2, 1 Mannheim 555 0.555 0.344877     215 3.583333
##        hours
## 1 0.05972222

Contourplot - preparing the data

library(geosmdata2)
xml_obj <- get_osm_nodes("bakery","Berlin")
info <- extract_info_op(xml_obj,"bakery")

Contourplot - get the map

library(ggmap)
Imap <- qmap(location = "Berlin", zoom = 10, maptype="toner",source="stamen")
Imap

Make first contourplot

The example is taken from here

Imap + geom_density2d(data = info, aes(x = lon, y = lat),lwd=1.5)

Visualize with hexbins

Another ggmap example